<?php
/**
 * User: b179a9
 * Date: 05.09.11
 * Time: 16:30
 */

class App_Form_Payment_Form extends Zend_Form
{
    private $monthes = array();
    private $years = array();

    private $cardTypes = array('visa', 'mastercard', 'amex', 'discover');

    public function init()
    {
        parent::init();
        
        $this->initMonthes();
        $this->initYears();

        $this->setAction('/payment/direct');
        $this->setMethod('post');

        $money = new Zend_Form_Element_Text('money');
        $money->setLabel('Money to buy:')
            ->setRequired()
            ->addValidator(new Zend_Validate_Digits());
        $this->addElement($money);

        $type = new Zend_Form_Element_Select('type');
        $type->setLabel('Credit card type');
        $type->setRequired();
        $type->setAttrib('placeholder', 'Credit card type');
        foreach($this->cardTypes as $card) {
            $type->addMultiOption($card, $card);
        }
        $type->addValidator(new Zend_Validate_InArray($this->cardTypes));
        $type->addErrorMessages(array(Zend_Validate_InArray::NOT_IN_ARRAY => 'Please choose right card type.'));
        $this->addElement($type);

        $number = new Zend_Form_Element_Text('number');
        $number->setRequired();
        $number->addValidator(new Zend_Validate_Digits());
        $number->setAttrib('placeholder', 'Credit card number');
        $number->setLabel('Credit card number');
        $this->addElement($number);

        $month = new Zend_Form_Element_Select('month');
        $month->setRequired();
        $month->addValidator(new Zend_Validate_InArray($this->monthes));
        $month->setErrorMessages(array(Zend_Validate_InArray::NOT_IN_ARRAY => 'Please, choose a month from the list.'));
        $month->setAttrib('placeholder', 'Expiration month');
        foreach($this->monthes as $mnth) {
            $month->addMultiOption($mnth, $mnth);
        }
        $month->setLabel('Expiration month');
        $this->addElement($month);

        $year = new Zend_Form_Element_Select('year');
        $year->setRequired();
        $year->setAttrib('placeholder', 'Expiration year');
        $year->addValidator(new Zend_Validate_InArray($this->years));
        $year->setErrorMessages(array(Zend_Validate_InArray::NOT_IN_ARRAY => 'Please, choose a year from the list.'));
        foreach($this->years as $yr) {
            $year->addMultiOption($yr, $yr);
        }
        $year->setLabel('Expiration year');
        $this->addElement($year);

        $firstname = new Zend_Form_Element_Text('firstname');
        $firstname->setRequired();
        $firstname->setAttrib('placeholder', 'First name');
        $firstname->setLabel('First name');
        $this->addElement($firstname);

        $lastname = new Zend_Form_Element_Text('lastname');
        $lastname->setRequired();
        $lastname->setAttrib('placeholder', 'Last name');
        $lastname->setLabel('Last name');
        $this->addElement($lastname);

        $address1 = new Zend_Form_Element_Text('address1');
        $address1->setRequired();
        $address1->setAttrib('placeholder', 'Address');
        $address1->setLabel('Address');
        $this->addElement($address1);

//        $address2 = new Zend_Form_Element_Text('address2');
//        $address2->setAttrib('placeholder', 'Address 2');
//        $this->addElement($address2);

        $city = new Zend_Form_Element_Text('city');
        $city->setRequired();
        $city->setAttrib('placeholder', 'City');
        $city->setLabel('City:');
        $this->addElement($city);

        $state = new Zend_Form_Element_Text('state');
        $state->setRequired();
        $state->setAttrib('placeholder', 'State (two letters)');
        $state->setLabel('State (two letters)');
        $this->addElement($state);

        $zip = new Zend_Form_Element_Text('zip');
        $zip->setRequired();
        $zip->addValidator(new Zend_Validate_Digits());
        $zip->setAttrib('placeholder', 'Zip code');
        $zip->setLabel('Zip code');
        $this->addElement($zip);

        $country = new Zend_Form_Element_Text('country');
        $country->setRequired();
        $country->setAttrib('placeholder', 'Country');
        $country->setLabel('Country');
        $this->addElement($country);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Выполнить');
        $submit->setOptions(array('class' => 'buybutton btn'));
        $this->addElement($submit);

        $this->setDecorators(array(array('ViewScript', array('viewScript' => '/forms/payment/form.phtml'))));


    }

    private function initMonthes() {
        $this->monthes = range(1, 12);
    }

    private function initYears() {
        $current = date('Y');
        $this->years = range($current, $current + 6);
    }

}
